using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DocLibApp
{
    public partial class FormRole : FormBase
    {
        public FormRole()
        {
            InitializeComponent();
        }

        public void CreateNewRole()
        {
            this.Text = "Create new security role";
        }

        public void EditRole(string role)
        {
            this.Text = "Edit security role [" + role + "]";

            LoadRoleInfo(role);
        }

        private void LoadRoleInfo(string role)
        {
            //header
            System.Collections.Hashtable htHeader = doc_security.Get_Role_Header(role);
            LoadDataFromHashToControls(groupBoxRoleHeader.Controls, htHeader);
            //role procedure list
            List<string> ProcedureList = doc_security.Get_Procedure_For_Role(role);
            lsvProcedure.ShowData(ProcedureList);
        }

        private void SaveRole()
        {
            wait_start("Saving security role information ...");

            string role = txtRoleName.Text.Trim();
            string desc = txtDescription.Text.Trim();
            List<string> ProcedureList = lsvProcedure.SaveItemsIntoStringList();

            string message = doc_security.Role_Save(role, desc, ProcedureList);

            wait_end();

            ShowMessage(message);
            this.Close();
        }

        private void btnSaveRole_Click(object sender, EventArgs e)
        {
            SaveRole();
        }

        private void btnChange_Click(object sender, EventArgs e)
        {
            string proc = txtProcedureName.Text.Trim();

            if (!string.IsNullOrEmpty(proc) && !lsvProcedure.IsItemAlreadyInList(proc))
            {
                ListViewItem aitem = new ListViewItem(proc);
                aitem.Tag = proc;
                lsvProcedure.Items.Add(aitem);
            }
        }
    }
}